﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LINQAmazonProvider;
using Common.DataTypes;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Threading;
using System.Threading; // Este using es necesario para que no de error de compilacion ya que sino 
                   // piensa que se va por el metodo de estencion de operadores


namespace SearchEngine
{
    public class Searcher
    {
        //Dispatcher of UiThread 
        private Dispatcher _mainDispatcher;
        // Amazon Search parameter
        static string searchKeyword;

        public Searcher(Dispatcher mainDispatcher)
        {
            _mainDispatcher = mainDispatcher;
        }

        public void PerformSearch(string keyword)
        {
            try
            {
                // Create thread to perform the amazon search
                Thread worker = new Thread(new ThreadStart(GetLINQResult));

                // Set the static parameter Keyword
                searchKeyword = keyword;
                worker.Start();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private void GetLINQResult()
        {
            try
            {

                List<Item> result = new List<Item>();

                var provider = new AmazonProvider();

                var queryable = new AmazonQuery<Common.DataTypes.Item>(provider);

                var query =
                    from book in queryable
                    where
                      book.Keyword == searchKeyword
                    select book;

                foreach (var item in query)
                {
                    result.Add(item);
                }

                //Invoque the event on the UiThread
                _mainDispatcher.BeginInvoke(delegate()
                {
                    OnSearchCompleted(new SearchCompletedEventArgs(result));
                });
            }
            catch (Exception e)
            {
                throw e;
            }
            
        }

        public delegate void SearchCompletedHandler(object sender, SearchCompletedEventArgs e);

        public event SearchCompletedHandler SearchCompleted;

        // Invoke the Changed event; called whenever list changes
        protected virtual void OnSearchCompleted(SearchCompletedEventArgs e)
        {
            if (SearchCompleted != null)
                SearchCompleted(this, e);
        }
        
    }


    public class SearchCompletedEventArgs
    {
        
        List<Item> _result = new List<Item>();
        
        public SearchCompletedEventArgs(List<Item> result)
        {
            _result = result;
        }

        public List<Item> Result
        {
            get
            {
                return _result;
            }
        }
    }

}

